package com.example.imtoken.modules.main.wallet.assets

import android.content.Intent
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ScreenUtils
import com.example.imtoken.R
import com.example.imtoken.core.App
import com.example.imtoken.core.base.BaseFragment
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.AssetsFragmentBinding
import com.example.imtoken.modules.main.wallet.details.WalletDetailsActivity
import com.example.imtoken.modules.main.wallet.details.trade.code.ReceivingCodeActivity
import com.example.imtoken.modules.main.wallet.details.trade.transfer.TransferActivity
import com.yanzhenjie.recyclerview.Controller2
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import com.yanzhenjie.recyclerview.SwipeMenuLayout
import kotlinx.android.synthetic.main.assets_fragment.*

/**
 * 资产
 * @property adapter AssetsAdapter
 */
class AssetsFragment :
    BaseFragment<AssetsViewModel, AssetsFragmentBinding>(AssetsViewModel::class.java) {

    private lateinit var adapter: AssetsAdapter

    override fun initLayoutId(): Int {
        return R.layout.assets_fragment
    }

    override fun init() {
        BusUtils.register(this)
        initAdapter()
        initObServe()
        initListener()
        viewModel.getWalletAddress()
    }

    @BusUtils.Bus(tag = BusConfig.update_wallet_address_sticky_assets_fragment, sticky = true)
    fun updateWalletAddress() {
        viewModel.getWalletAddress()
    }

    private fun initListener() {
        srlAssets.setOnRefreshListener {
            viewModel.getWalletAddress()
        }
    }

    private fun initAdapter() {
        adapter = AssetsAdapter(R.layout.item_assets_view)
        val layoutManager = LinearLayoutManager(context)

        rvAssets.setOnSwipeController2(object : Controller2 {
            override fun leftSwipeFunc(menuLayout: SwipeMenuLayout, p: Int) {
                //收款二维码
                BusUtils.postSticky(
                    BusConfig.out_data_sticky_receiving_code_activity,
                    viewModel.getDataLiveData().value?.get(p)
                )
                val intent = Intent(context, ReceivingCodeActivity::class.java)
                when (viewModel.getDataLiveData().value?.get(p)?.walletType) {
                    "eth", "usdt" -> {
                        if (viewModel.getDataLiveData().value?.get(p)?.fatherAddress == viewModel.getDataLiveData().value?.get(
                                p
                            )?.address
                        ) {
                            intent.putExtra("title", "ETH")
                        } else {
                            intent.putExtra("title", "USDT")
                        }
                    }
                    "trx", "usdj" -> {
                        if (viewModel.getDataLiveData().value?.get(p)?.fatherAddress == viewModel.getDataLiveData().value?.get(
                                p
                            )?.address
                        ) {
                            intent.putExtra("title", "TRX")
                        } else {
                            intent.putExtra("title", "USDJ")
                        }
                    }
                }
                ActivityUtils.startActivity(intent)

                menuLayout.postDelayed({
                    menuLayout.smoothCloseMenu()
                }, 300)
            }

            override fun rightSwipeFunc(menuLayout: SwipeMenuLayout, p: Int) {
                //转账
                val intent = Intent(activity, TransferActivity::class.java)
                intent.putExtra(
                    "title",
                    viewModel.getDataLiveData().value?.get(p)?.walletType?.toUpperCase()
                )
                intent.putExtra("coin", App.createLiveData.value?.coin?.coin)
                ActivityUtils.startActivity(intent)
                menuLayout.postDelayed({
                    menuLayout.smoothCloseMenu()
                }, 300)
            }

        })
        rvAssets.setSwipeMenuCreator(mSwipeMenuCreator)
        rvAssets.layoutManager = layoutManager
        rvAssets.adapter = adapter
        adapter.setOnItemClickListener { _, _, position ->
            BusUtils.postSticky(
                BusConfig.out_data_sticky_wallet_details_activity,
                adapter.data[position]
            )
            val intent = Intent(activity, WalletDetailsActivity::class.java)
            ActivityUtils.startActivity(intent)
        }
    }

    private val mSwipeMenuCreator =
        SwipeMenuCreator { leftMenu, rightMenu, _ ->
            val func1 = SwipeMenuItem(context)
            func1.text = getString(R.string.receiving)
            func1.width = ScreenUtils.getScreenWidth() / 2
            func1.setBackgroundColorResource(R.color.receivePaymentColor)
            func1.setTextColorResource(R.color.white)
            func1.height = LinearLayout.LayoutParams.MATCH_PARENT
            leftMenu?.addMenuItem(func1)

            val func2 = SwipeMenuItem(context)
            func2.text = getString(R.string.transfer)
            func2.width = ScreenUtils.getScreenWidth() / 2
            func2.setBackgroundColorResource(R.color.wallet_1)
            func2.setTextColorResource(R.color.white)
            func2.height = LinearLayout.LayoutParams.MATCH_PARENT
            rightMenu?.addMenuItem(func2)
        }

    private fun initObServe() {
        viewModel.getDataLiveData().observe(this) {
            adapter.setList(it)
        }
        viewModel.refreshStatus.observe(this) {
            dataBinding.srlAssets.isRefreshing = it
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.removeSticky(BusConfig.update_wallet_address_sticky_assets_fragment)
        BusUtils.unregister(this)
    }

}